//
//  JMMembersVController.m
//  jiMuOperate
//
//  Created by 胡梅方 on 16/5/3.
//  Copyright © 2016年 jinXinFengLi. All rights reserved.
//

#import "JMMembersVController.h"
#import "MJRefresh.h"
#import "JMMembersTViewCell.h"
#import "JMMembersListReturn.h"

NSString *const JMTableViewCellIdentifier = @"JMMembersCell";
@interface JMMembersVController ()<UITableViewDataSource,UITableViewDelegate>
{
    int _pagNum;
    NSString * _token;
}
@property(nonatomic,weak)UIImageView *headImage;
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,weak)UIButton *backBtn;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation JMMembersVController

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    HIDDENNAV(YES);

    [self createViews];

      [self setupRefresh];
    _token = [UserEntity shardUserInfo].access_token;

}
-(void)createViews
{
    UIImageView *headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 180)];
    headImage.image = [UIImage imageNamed:@"banner_member"];
    headImage.backgroundColor = [UIColor whiteColor];
    self.headImage = headImage;
    headImage.frame = [ToolViewAndData MyAutoLayout:headImage];
    [self.view addSubview:headImage];
    
    UITableView *stageTableView = [[UITableView alloc]initWithFrame:CGRectMake(0 ,180, 320, 388)];
    self.tableView = stageTableView;
       [self.tableView registerClass:[JMMembersTViewCell class] forCellReuseIdentifier:JMTableViewCellIdentifier];
    stageTableView.delegate = self;
    stageTableView.dataSource = self;
    stageTableView.frame = [ToolViewAndData MyAutoLayout:stageTableView];
    [self.view addSubview:stageTableView];
    UIButton *backBtn = [ARC createButtonWithFrame:CGRectMake(0, 20, 50, 30) Target:self Sel:@selector(backToLast:)];
    [backBtn setImage:[UIImage imageNamed:@"aut_ico_back"] forState:UIControlStateNormal];
    backBtn.frame = [ToolViewAndData MyAutoLayout:backBtn];
    [self.view addSubview:backBtn];
}
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //warning 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.backgroundColor = [UIColor grayColor];
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableView.headerRefreshingText = @"正在帮你刷新中,不客气";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"正在帮你加载中,不客气";
  
}

//下拉刷新数据
- (void)headerRereshing
{
    _pagNum = 7;
    
    [self requestGetTeamDataWithToken:_token page:1 pageSize:_pagNum IsHeaderR:YES];
}

-(void)footerRereshing
{
    _pagNum += 2;
    [self requestGetTeamDataWithToken:_token page:1 pageSize:_pagNum IsHeaderR:NO];
    

}

-(void)requestGetTeamDataWithToken:(NSString*)token page:(NSInteger)page pageSize:(NSInteger)pagesize IsHeaderR:(BOOL)isHeaderR
{
    NSString *url = [NSString stringWithFormat:@"%@%@&page=%ld&pageSize=%ld",JMURLGetCommuneUserList,token,(long)page,(long)pagesize];
    NSArray *arrayR = @[@"",url,@"GET"];
    [Request startRequestWithArray:arrayR FinishedBlock:^(NSData *data) {
        NSDictionary *dict = [JMData dictAndTestData:data];
        JMMembersListReturn *membersList = [JMMembersListReturn objectWithKeyValues:dict];
        
        if ([membersList.State intValue]>= 0) {
          self.dataArray = [JMMembersListInfo objectArrayWithKeyValuesArray:membersList.data];

            
            NSMutableArray *aryM = [NSMutableArray array];
            for (JMMembersListInfo *membersInfo00 in aryM) {
                if (![self.dataArray containsObject:membersInfo00]) {
                    [aryM addObject:membersInfo00];
                }
            }
            [self.dataArray addObjectsFromArray:aryM];
            if (isHeaderR) {
                [self.tableView headerEndRefreshing];
            }else{
                [self.tableView footerEndRefreshing];
            }


            [self.tableView reloadData];
        }
        
    } FailBlock:^(NSString *string) {
        SHOWALERT(@"服务器繁忙，请梢后重试");
    }];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    HIDDENNAV(YES);
}

-(void)backToLast:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JMMembersTViewCell *cell =(JMMembersTViewCell*) [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[JMMembersTViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:JMTableViewCellIdentifier];
        
        JMMembersListInfo *membersInfo = self.dataArray[indexPath.row];
        [JMData requestOrSetImgWithImageView:cell.personImage ProductImageUrl:membersInfo.HeadImg];
        cell.gradeNumber.text = membersInfo.Sort;
        if (membersInfo.NickName == NULL) {
            membersInfo.NickName = @"钱要多多";
        }
        cell.nameLabel.text = membersInfo.NickName;
        
        cell.levelLab.text = [NSString stringWithFormat:@"级别:%@",membersInfo.Level];
        cell.gradeNumber.textColor = [UIColor blackColor];
        
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60*SCREEN_HEIGHT/568;
}
@end
